package com.cincc.taofangwang.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.taofangwang.R;
import com.cincc.taofangwang.common.MyUtils;
import com.cincc.taofangwang.common.callback.OnClickCallBack;
import com.cincc.taofangwang.model.RecordModel;

import java.util.List;

public class RecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<RecordModel> list;

    private static final int HEADER_ITEM = 0;
    private static final int DATA_ITEM = 1;

    public RecordAdapter(Context context, List<RecordModel> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return HEADER_ITEM;
        else return DATA_ITEM;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HEADER_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.layout_record_header, parent, false);
            return new HeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.layout_record_item, parent, false);
            return new MyViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        RecordModel model = list.get(position);
        if (holder.getItemViewType() == HEADER_ITEM) {
            HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
            headerViewHolder.beginTimeText.setText(MyUtils.getCurrentTime(model.searchBeginTime));
            headerViewHolder.endTimeText.setText(MyUtils.getCurrentTime(model.searchEndTime));
            headerViewHolder.purposeText.setText(model.searchPurpose);
            headerViewHolder.statusText.setText(model.searchStatus);
            headerViewHolder.numberEdit.setText(model.searchNumber);
            headerViewHolder.beginTimeText.setOnClickListener(v -> {
                onClickBtnBack.callback(1);
            });
            headerViewHolder.endTimeText.setOnClickListener(v -> {
                onClickBtnBack.callback(2);
            });
            headerViewHolder.purposeText.setOnClickListener(v -> {
                onClickBtnBack.callback(3);
            });
            headerViewHolder.statusText.setOnClickListener(v -> {
                onClickBtnBack.callback(4);
            });
            headerViewHolder.searchLayout.setOnClickListener(v -> {
                onClickBtnBack.callback(5);
            });
            headerViewHolder.numberEdit.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    onTextChangeBack.callback(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        } else {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            myViewHolder.nameText.setText(TextUtils.isEmpty(model.name) ? "用户" : model.name);
            myViewHolder.numberText.setText(model.number);
            myViewHolder.statusText.setText(model.purposeFmt + " " + model.statusFmt);
            myViewHolder.dialLayout.setOnClickListener(v -> {
                onClickBallBack.callback(position);
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nameText;
        TextView numberText;
        TextView statusText;
        LinearLayout dialLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            nameText = itemView.findViewById(R.id.name_tv);
            numberText = itemView.findViewById(R.id.number_tv);
            statusText = itemView.findViewById(R.id.status_tv);
            dialLayout = itemView.findViewById(R.id.dial_layout);
        }
    }

    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView beginTimeText;
        TextView endTimeText;
        TextView purposeText;
        TextView statusText;
        EditText numberEdit;
        RelativeLayout searchLayout;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            beginTimeText = itemView.findViewById(R.id.begin_time_tv);
            endTimeText = itemView.findViewById(R.id.end_time_tv);
            purposeText = itemView.findViewById(R.id.edit_purpose);
            statusText = itemView.findViewById(R.id.edit_status);
            numberEdit = itemView.findViewById(R.id.edit_number);
            searchLayout = itemView.findViewById(R.id.search_btn);
        }
    }

    private OnClickCallBack onClickBallBack;

    public void setCallBack(OnClickCallBack callBack) {
        this.onClickBallBack = callBack;
    }


    public interface OnClickBtnBack {
        void callback(int i);
    }

    private OnClickBtnBack onClickBtnBack;

    public void setClickBtnBack(OnClickBtnBack callBack) {
        this.onClickBtnBack = callBack;
    }

    public interface OnTextChangeBack {
        void callback(String i);
    }

    private OnTextChangeBack onTextChangeBack;

    public void setOnTextChangeBack(OnTextChangeBack callBack) {
        this.onTextChangeBack = callBack;
    }

}
